
#include"UserPanel.h"
#include"TextQuery.h"
#include"InputParser.h"

vector<char> choice{'1','2','3','q'};

UserPanel::UserPanel(TextQuery &tq)
:p_textquery_(make_shared<TextQuery>(tq)),p_ipaser_(make_shared<InputParser>(InputParser(tq)))
{    
    loadHistory();
    char choice;
    do{
        showMenu();
        cin >> choice;
        makeChoice(choice);
    }while(choice != 'q');
}

string UserPanel::getInput(){
    string str;
    getline(cin,str);
    auto it = remove(str.begin(),str.end(),' ');
    str.erase(it,str.end());
    return str;
}

void UserPanel::showMenu(){
    cout << "________________________" << endl;
    cout << choice[0] <<  ". show text of data" << endl;
    cout << choice[1] <<  ". search word" << endl;
    cout << choice[2] <<  ". search from history" << endl;
    cout << choice[3] << ".  quit" << endl;
    cout << "________________________" << endl;

}

void UserPanel::makeChoice(const char &ch){
    while(getchar() != '\n')
        continue;
    if(ch == choice[0]){
        showText();
    }
    else if(ch == choice[1]){
        runSearch();
    }
    else if(ch == choice[2]){
        searchHistory();
    }
    else if(ch == choice[3]){
        
    }
    else{
        cout << "invaild input!" << endl;
    }
}

void UserPanel::showText(){
    cout << "show text: " << endl;
    p_textquery_->showText();
    cout << "text ends " << endl; 
}

void UserPanel::runSearch(){
    string word;
    cout << "input the pattern you want to search(type none to quit): " << endl;
    word = getInput();
    while(!word.empty()){
        if(!checkQueryInp(word))
            cout << "invaild query!" << endl;
        else{
            p_ipaser_->execQueries(word);
            history_.push_back(word);
        }
        cout << "input the pattern you want to search(type none to quit): " << endl;
        word = getInput();
    }
}

bool UserPanel::checkQueryInp(const string &str){
    string brkstr;
    for(auto it = str.begin(); it != str.end(); it++){
        if(*it == '(' || *it == ')')
            brkstr.push_back(*it);
        else if(*it == '~'){
            continue;
        }
        else if(*it  == '&' || *it  == '|'){
            continue;
        }
        else if(!isalpha(*it))
            return false;        
    }
    stack<char> brksta;
    for(char ch : brkstr){
        if(ch == '(')  brksta.push(')');
        else if(brksta.empty() || brksta.top() != ch)   return false;
        else brksta.pop();        
    }
    return brksta.empty();
}

void UserPanel::searchHistory(){
    auto showHistory = [&](){
        int idx = 1;
        for(string str : history_){
            cout << idx++ << "." << str << endl;
        }
        cout << endl;
    };
    showHistory();
    char c;
    cout << "input the query idx you want search(q to quit):" << endl;
    cin >> c;
    while(c != 'q'){
        int num = c - '0';
        if(num > history_.size() || num < 1)
            cout << "invaild idx!" << endl;
        else{
            string str = history_[num - 1];
            p_ipaser_->execQueries(str);
        } 
        showHistory();
        cout << "input the query idx you want search(q to quit):" << endl;
        cin >> c;   
    }
    
}

void UserPanel::refresh(){
    system("clear");
}

void UserPanel::pause(){
    system("read -r -p \"Press any key to continue...\" key");
}

void UserPanel::loadHistory(){
    
}